//
//  SLDemo_KVOController.m
//  Objective-C
//
//  Created by S_Line on 2018/6/6.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLDemo_KVOController.h"

#import "SLTeacher.h"

@interface SLDemo_KVOController ()

@property (nonatomic, strong) SLTeacher *teacher;

@property (nonatomic, weak) UILabel *label1;

@end

@implementation SLDemo_KVOController

- (void)initNavBar {
    self.navTitle = @"KVO";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initData];
    
    [self initView];
}

- (void)initData {
    SLTeacher *teacher = [[SLTeacher alloc] init];
    self.teacher = teacher;
    teacher.tid = @1;
    teacher.name = @"张三";
    teacher.age = 45;
    SLBook *teacherBook = [[SLBook alloc] init];
    teacherBook.bookName = @"语文";
    teacher.book = teacherBook;
    // void *任意指针, 相当于id
    [teacher addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"测试"];
}

- (void)initView {
    UILabel *label1 = [[UILabel alloc] init];
    self.label1 = label1;
    label1.font = [UIFont systemFont13];
    label1.backgroundColor = [UIColor whiteColor];
    label1.numberOfLines = 0;
    label1.text = [self.teacher description];
    [self.view addSubview:label1];
    [label1 mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(10 + kNavigationBarHeight);
    }];
    
    UIButton *changeButton = [[UIButton alloc] init];
    [changeButton setTitle:@"改变名字" forState:UIControlStateNormal];
    [changeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [changeButton addTarget:self action:@selector(changeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeButton];
    [changeButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label1.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(40);
    }];
}

#pragma mark ----- 响应事件
- (void)changeButtonClick:(UIButton *)sender {
    self.teacher.name = @"测试";
}

/**
 <#Description#>

 @param keyPath kayPath
 @param object 被监听的对象(teacher)
 @param change 改变的值{kind = 1; new = "李四"; old = "张三"}
 @param context 监听时添加的context
 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([@"name" isEqualToString:keyPath]) {
        SLog(@"object: %@, change: %@, context: %@", object, change, context);
        SLog(@"new: %@", [change valueForKey:@"new"]);
        self.label1.text = [self.teacher description];
    }
}

- (void)watchTeacherForChageName:(SLTeacher *)teacher {
    [teacher addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)dealloc {
    [self.teacher removeObserver:self forKeyPath:@"name"];
}

@end
